package dao;

import entity.User;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class UserDao extends BaseDao{

    //列表所有用户信息
    public List getAllUsers() throws Exception {
        List userList = new ArrayList();
        String sql = "select userId,userName,cardBalance,identity,userPassword" +
                " from user";
        Connection connection = getConnection();//获取数据库连接
        Statement statement = connection.createStatement();//创建statement对象
        ResultSet resultSet = statement.executeQuery(sql);//执行查询sql
        while(resultSet.next()){
            String userId = resultSet.getString("userId");
            String userName = resultSet.getString("userName");
            float cardBalance = resultSet.getFloat("cardBalance");
            String identity = resultSet.getString("identity");
            String userPassword = resultSet.getString("userPassword");
            User user = new User(userId,userName,userPassword,cardBalance,identity);
            userList.add(user);
        }
        return userList;
    }
    //用户登录验证
    public User login(String id) throws Exception {
        String sql = "select userId,userName,cardBalance,identity,userPassword" +
                " from user where userId=" + id;
        Connection connection = getConnection();//获取数据库连接
        Statement statement = connection.createStatement();//创建statement对象
        ResultSet resultSet = statement.executeQuery(sql);//执行sql语句
        User user = null;

        while(resultSet.next()){//遍历链表集
            String userId = resultSet.getString("userId");
            String userName = resultSet.getString("userName");
            Float cardBalance = resultSet.getFloat("cardBalance");
            String identity = resultSet.getString("identity");
            String userPassword = resultSet.getString("userPassword");

            user = new User(userId,userName,userPassword,cardBalance,identity);
        }
        return user;
    }


    //录入用户信息
    public boolean addUser(User user) throws Exception {
        String sql = "insert into user(userId,userName,userPassword,cardBalance,identity)" +
                " VALUES(?,?,?,?,?)";
        Connection connection = getConnection();//获取数据库连接
        PreparedStatement preparedStatement = connection.prepareStatement(sql);//创建PreparedStatement
        preparedStatement.setString(1, user.getUserId());
        //设置第一个？占位符的值
        preparedStatement.setString(2, user.getUserName());
        //设置第二个？占位符的值
        preparedStatement.setString(3, user.getUserPassword());
        //设置第三个？占位符的值
        preparedStatement.setFloat(4, user.getCardBalance());
        //设置第四个？占位符的值
        preparedStatement.setString(5, user.getIdentity());
        //设置第五个？占位符的值
        int num = preparedStatement.executeUpdate();//执行sql语句
        if (num==1)
        {
            return true;
        }else {
            return false;
        }
    }

    //修改用户信息
    public boolean updateUser(User user) {
        int i = 0;
        try{
            Connection connection = getConnection();//获取连接
            String updatesql = "update user set userName=?,"
                    +"userPassword=?,"
                    +"cardBalance=?,"
                    +"identity=?"
                    +" where userId=?";
            PreparedStatement preparedStatement = connection.prepareStatement(updatesql);
            //设置？占位符的值
            preparedStatement.setString(1,user.getUserName());
            preparedStatement.setString(2,user.getUserPassword());
            preparedStatement.setFloat(3,user.getCardBalance());
            preparedStatement.setString(4,user.getIdentity());
            preparedStatement.setString(5,user.getUserId());
            i = preparedStatement.executeUpdate();//执行修改sql语句
        }catch (Exception e){
            e.printStackTrace();
        }
        if (i == 1){
            return true;//执行成功
        }else {
            return false;//执行失败
        }
    }
    //删除用户
    public String deleteUser(User user) throws Exception {
        String result= null;
        Connection connection = getConnection();//获取数据库连接

        String sql = "delete from user where userId=?";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1,user.getUserId());
        int num = preparedStatement.executeUpdate();//执行删除sql操作
        if(num==1){
            //删除成功
            result = "删除成功";
        }else {
            //删除失败
            result = "删除失败";
        }
        return result;
    }

    public static void main(String[] args) throws Exception {
        UserDao userDao = new UserDao();
        List userList = userDao.getAllUsers();
        System.out.println(userList);
    }
}
